var resetDemoData = function(){ 
  Meteor.users.remove({});
  Meteor.roles.remove({});

  Chats.remove({});
  Messages.remove({});

  DefaultOptions.remove({});

  Companies.remove({});
  Employees.remove({});
  Departments.remove({});

  ProductCatalogs.remove({});
  ProductPropertyGroups.remove({});
  Products.remove({});

  SaleOrders.remove({});

  Inventory.remove({});

  Suppliers.remove({});

  Purchase.remove({});
}

// 演示账号
//
// 以下为模拟数据，仅做数据展示

if(Meteor.users.find().count() === 0){
  var demoAccount = [{
    _id:'1',
    username:'bg',
    emails:[{address:'example1@gmail.com'}],
    profile: {
      name:'李健',
      sex: '男',
      birthday:moment([1985,6,8]).toDate(),
      address:{
        province:'广东',
        city:'深圳市',
        region:'南山区',
        street:'南油小区660室'
      },
      phones:[{label:'工作',number:'13856585255'},{label:'备用',number:'13613525212'}],
      email:'cxvssadas@163.com',
      departments:[{_id:'1',type:'总经部',name:'总经部'}],
      roleName:'老板',
      state:'在职'
    },
    uuid:'359786050811112',
    company_id:'1',
    state:'已启用'
  },{
    _id:'2',
    username:'storemanager',
    emails:[{address:'example2@gmail.com'}],
    profile: {
      name:'刘萌',
      sex: '女',
      birthday:moment([1990,2,12]).toDate(),
      address:{
        province:'广东省',
        city:'深圳市',
        region:'南山区',
        street:'南油小区502室'
      },
      phones:[{label:'工作',number:'13856585255'},{label:'备用',number:'13613525212'}],
      email:'cxvas@163.com',
      departments:[{_id:'2',type:'门店组',name:'门店'},{_id:'3',type:'门店',name:'南山店'}],
      roleName:'店长',
      state:'在职'
    },
    uuid:'359786050811117',
    company_id:'1',
    state:'已启用'
  },{
    _id:'3',
    username:'sales',
    emails:[{address:'example3@gmail.com'}],
    profile: {
      name:'玳瑁',
      sex: '男',
      birthday:moment([1986,3,21]).toDate(),
      address:{
        province:'广东省',
        city:'深圳市',
        region:'南山区',
        street:'南油小区525室'
      },
      phones:[{label:'工作',number:'13856585255'},{label:'备用',number:'13613525212'}],
      email:'asxc@163.com',
      departments:[{_id:'2',type:'门店组',name:'门店'},{_id:'3',type:'门店',name:'南山店'},{_id:'4',type:'销售部',name:'销售1部'},{_id:'11',type:'柜组',name:'柜组1'}],
      roleName:'销售员',
      state:'在职',
      remark:''
    },
    uuid:'3597860502121112',
    company_id:'1',
    state:'已启用'
  },{
    _id:'4',
    username:'whm',
    emails:[{address:'example4@gmail.com'}],
    profile: {
      name:'林萧',
      sex: '男',
      birthday:moment([1988,6,12]).toDate(),
      address:{
        province:'广东省',
        city:'深圳市',
        region:'南山区',
        street:'南油小区780室'
      },
      phones:[{label:'工作',number:'13856666666'},{label:'备用',number:'13613525212'}],
      email:'linxiao@163.com',
      departments:[{_id:'6',type:'仓库',name:'总仓'}],
      roleName:'仓管员',
      state:'在职',
      remark:''
    },
    uuid:'31893476811117',
    company_id:'1',
    state:'已启用'
  },{
    _id:'5',
    username:'casher',
    emails:[{address:'example5@gmail.com'}],
    profile: {
      name:'贺佳佳',
      sex: '女',
      birthday:moment([1984,6,21]).toDate(),
      address:{
        province:'广东省',
        city:'深圳市',
        region:'南山区',
        street:'南油小区7820室'
      },
      phones:[{label:'工作',number:'13856585255'},{label:'备用',number:'13613525212'}],
      email:'xcv_sds@163.com',
      departments:[{_id:'2',type:'门店组',name:'门店'},{_id:'3',type:'门店',name:'南山店'}],
      roleName:'收银员',
      state:'在职',
      remark:''
    },
    uuid:'31893476811117',
    company_id:'1',
    state:'未启用'
  },{
    _id:'6',
    username:'13888885255',
    emails:[{address:'example6@gmail.com'}],
    profile: {
      name:'罗晓琳',
      sex: '女',
      birthday:moment([1984,6,21]).toDate(),
      address:{
        province:'广东省',
        city:'深圳市',
        region:'南山区',
        street:'南油小区1120室'
      },
      phones:[{label:'工作',number:'13888885255'},{label:'备用',number:'13613525212'}],
      email:'xcv_sds@163.com',
      departments:[{_id:'2',type:'门店组',name:'门店'},{_id:'3',type:'门店',name:'南山店'},{_id:'4',type:'销售部',name:'销售1部'},{_id:'12',type:'柜组',name:'柜组2'}],
      roleName:'销售员',
      state:'离职',
      remark:''
    },
    uuid:'31893476811117',
    company_id:'1',
    state:'已停用'
  }];

  var demoAccountRoles = [{
    roles:[
      // 库存权限
    ],
  },{
    roles:[
      'storeOrdersPub','createSaleOrder'
    ],
  },{
    roles:[
      'salesManOrdersPub','createSaleOrder'
    ],
  },{
    roles:['warehouse-keeper'],
  },{
    roles:['casher'],
  },{
    roles:['sales-man'],
  }];
    demoAccount.forEach(function(acc,index){
      Meteor.users.insert(acc);
      Accounts.setPassword(acc._id, '888888');
      Roles.addUsersToRoles(acc._id, demoAccountRoles[index].roles);
    });
}

// 主账号与公司信息
if(Companies.find().count() === 0){
  Companies.insert({
    _id: '1',
    name:'深圳市宾果互动科技有限公司',
    tel:'0755-86858545',
    address:{
      province:'广东省',
      city:'深圳市',
      region:'南山区',
      street:'南海大道海王大厦A座19F1'
    },
    enabled_time:moment().toDate(),
    expiration:moment().add(366,'days').toDate(),
    users_num:30
  });

  Companies.insert({
    _id: '2',
    name:'华为技术有限公司有限公司',
    tel:'0755-52652545',
    address:{
      province:'广东省',
      city:'深圳市',
      region:'南山区',
      street:'南海大道660室'
    },
    enabled_time:moment().toDate(),
    expiration:moment().add(366,'days').toDate(),
    users_num:100
  });

  Companies.insert({
    _id: '3',
    name:'深圳市昌龙盛世资产管理有限公司',
    tel:'13537550025',
    address:{
      province:'广东省',
      city:'深圳市',
      region:'南山区',
      street:'南海大道海王大厦'
    },
    enabled_time:moment([2015, 8, 29]).toDate(),
    expiration:moment([2015, 8, 29]).add(366,'days').toDate(),
    users_num:30
  });

  Companies.insert({
    _id: '4',
    name:'深圳豪尊科技有限公司',
    tel:'0755-86858545',
    address:{
      province:'广东省',
      city:'深圳市',
      region:'南山区',
      street:''
    },
    enabled_time:moment([2015, 10, 10]).toDate(),
    expiration:moment([2015, 10, 10]).add(366,'days').toDate(),
    users_num:30
  });
}


// 部门/店铺信息
if (Departments.find().count() === 0) {
  Departments.insert({
    _id:'1',
    company_id:'1',
    type:'总经部',
    name:'总经办',
    tel:'0755-86854512',
    address:{
      province:'广东省',
      city:'深圳市',
      region:'南山区',
      street:'南海大道2154号'
    },
    state: '启用',
    remark:''
  });

  Departments.insert({
    _id:'2',
    company_id:'1',
    type:'门店组',
    name:'门店',
    state: '启用',
    remark:''
  });

  Departments.insert({
    _id:'3',
    company_id:'1',
    parent_id:'2',
    parents:['2'],
    type:'门店',
    name:'南山店',
    tel:'0755-86854512',
    address:{
      province:'广东省',
      city:'深圳市',
      region:'南山区',
      street:'南海大道2154号'
    },
    state: '启用',
    remark:''
  });

  Departments.insert({
    _id:'11',
    company_id:'1',
    parent_id:'3',
    parents:['2','3'],
    type:'销售部',
    name:'销售1部',
    state: '启用',
    remark:''
  });

  Departments.insert({
    _id:'12',
    company_id:'1',
    parent_id:'11',
    parents:['2','3','11'],
    type:'柜组',
    name:'柜组1',
    state: '启用',
    remark:'' //备注
  });
  Departments.insert({
    _id:'13',
    company_id:'1',
    parent_id:'11',
    parents:['2','3','11'],
    type:'柜组',
    name:'柜组2',
    state: '启用',
    remark:''
  });


  Departments.insert({
    _id:'4',
    company_id:'1',
    parent_id:'2',
    parents:['2'],
    type:'门店',
    name:'福田店',
    tel:'0755-86854512',
    address:{
      province:'广东省',
      city:'深圳市',
      region:'福田区',
      street:'福华三路1119号'
    },
    state: '启用',
    remark:''
  });
  Departments.insert({
    _id:'14',
    company_id:'1',
    parent_id:'4',
    parents:['2','4'],
    type:'销售部',
    name:'销售1部',
    state: '启用',
    remark:''
  });
  Departments.insert({
    _id:'15',
    company_id:'1',
    parent_id:'14',
    parents:['2','4','14'],
    type:'柜组',
    name:'柜组1', //备注
    state: '启用',
    remark:''
  });
  Departments.insert({
    _id:'16',
    company_id:'1',
    parent_id:'14',
    parents:['2','4','14'],
    type:'柜组',
    name:'柜组2',
    state: '启用',
    remark:''
  });

  Departments.insert({
    _id:'5',
    company_id:'1',
    parent_id:'2',
    parents:['2'],
    type:'store',
    name:'罗湖店',
    tel:'0755-86854512',
    address:{
      province:'广东省',
      city:'深圳市',
      region:'罗湖区',
      street:'泥岗路30号'
    },
    state: '启用',
    remark:''
  });

  Departments.insert({
    _id:'17',
    company_id:'1',
    parent_id:'5',
    parents:['2','5'],
    type:'销售部',
    name:'销售1部',
    state: '启用',
    remark:''
  });

  Departments.insert({
    _id:'18',
    company_id:'1',
    parent_id:'17',
    parents:['2','5','17'],
    type:'柜组',
    name:'柜组1', //备注
    state: '启用',
    remark:''
  });
  Departments.insert({
    _id:'19',
    company_id:'1',
    parent_id:'17',
    parents:['2','5','17'],
    type:'柜组',
    name:'柜组2',
    state: '启用',
    remark:''
  });
  Departments.insert({
    _id:'6',
    company_id:'1',
    type:'仓库',
    name:'总仓',
    tel:'0755-86854512',
    address:{
      province:'广东省',
      city:'深圳市',
      region:'罗湖区',
      street:'泥岗路30号'
    },
    state: '启用',
    remark:''
  });
}


// 供应商
if (Suppliers.find().count()===0){
  Suppliers.insert({
    _id:'1',
    category:'常用供应商',
    company:'上海生一科技有限公司',
    address:{
      province:'上海市',
      city:'上海',
      region:'徐汇区',
      street:'桂平路481号'
    },
    tel:'020-25665585',
    contact_list:[{
      level:'主要',
      name:'周先生',
      phones:[{type:'工作',value:'1368888888'}],
      emails:[{type:'工作',value:'supplier@163.com'}]
    }],
    company_id:'1'
  });

  Suppliers.insert({
    _id:'2',
    category:'手机供应商',
    company:'深圳市鼎瀚电子有限公司',
    address:{
      province:'广东省',
      city:'深圳市',
      region:'南山区',
      street:'南海大道2154号'
    },
    tel:'020-25665585',
    contact_list:[{
      level:'主要',
      name:'王总',
      phones:[{type:'工作',value:'1368888888'}],
      emails:[{type:'工作',value:'supplier@163.com'}]
    }],
    company_id:'1'
  });

  Suppliers.insert({
    _id:'3',
    category:'配件供应商',
    company:'合肥源拓电脑科技有限公司',
    address:{
      province:'安徽省',
      city:'合肥市',
      region:'徐汇区',
      street:'桂平路481号'
    },
    tel:'020-25665585',
    contact_list:[{
      level:'主要',
      name:'李小姐',
      phones:[{type:'工作',value:'1368888888'}],
      emails:[{type:'工作',value:'supplier@163.com'}]
    },{
      level:'备用',
      name:'王小姐',
      phones:[{type:'工作',value:'1328888888'}],
      emails:[{type:'工作',value:'supplier2@163.com'}]
    }],
    company_id:'1'
  });
}

// 采购数据
if(Purchase.find().count()===0){
  Purchase.insert({
    'order_no':'JH2562352512',
    'date':moment([2016,2,12]).toDate(),
    'total_cost':58662,
    'paid':0,
    'warehouse':{'_id':'11','name':'柜组1'},
    'supplier':{'_id':'1','company':'上海生一科技有限公司'},
    'list':[],
    'state':1,
    'state_history':[
        {'date':moment([2016,2,12]).toDate(),'operation':{'state':1,'note':'采购计划'},'operator':{'_id':'1','name':'李健'}}
    ],
    'company_id':'1'
  });

  Purchase.insert({
    'order_no':'JH2565236525',
    'date':moment([2016,2,12]).toDate(),
    'total_cost':38662,
    'paid':38662,
    'warehouse':{'_id':'11','name':'柜组1'},
    'supplier':{'_id':'2','company':'深圳市瀚鼎科技有限公司'},
    'list':[],
    'state':3,
    'state_history':[
        {'date':moment([2016,2,12]).toDate(),'operation':{'state':2,'note':'已下单'},'operator':{'_id':'1','name':'李健'}},
        {'date':moment([2016,2,13]).toDate(),'operation':{'state':3,'note':'配送中'},'operator':{'_id':'2','name':'刘萌'}}
    ],
    'company_id':'1'
  });

  Purchase.insert({
    'order_no':'JH2566365589',
    'date':moment([2016,2,11]).toDate(),
    'total_cost':48522,
    'paid':0,
    'warehouse':{'_id':'6','name':'总仓'},
    'supplier':{'_id':'1','company':'合肥源拓电脑科技有限公司'},
    'list':[],
    'state':0,
    'state_history':[
        {'date':moment([2016,2,11]).toDate(),'operation':{'state':2,'note':'已下单'},'operator':{'_id':'1','name':'李健'}},
        {'date':moment([2016,2,11]).toDate(),'operation':{'state':0,'note':'已作废'},'operator':{'_id':'2','name':'刘萌'}}
    ],
    'return':{'state':'部分退','return_order':''},
    'company_id':'1'
  });

  Purchase.insert({
    'order_no':'JH256222365',
    'date':moment([2016,2,8]).toDate(),
    'total_cost':56362,
    'paid':5000,
    'warehouse':{'_id':'11','name':'柜组1'},
    'supplier':{'_id':'2','company':'深圳市意达电子科技有限公司'},
    'list':[{
      "_id" : "TAswBtWyKpw8CcHNC",
      "catalog" : "手机",
      "barcode" : "2524123456125",
      "properties" : {
        "brand" : {
          "name" : "品牌",
          "value" : "华为",
          "logo" : "huawei"
        },
        "model" : {
          "name" : "型号",
          "value" : "荣耀8(R1530)"
        },
        "color" : {
          "name" : "颜色",
          "value" : "白色"
        },
        "netType" : {
          "name" : "网络",
          "value" : "移动4G"
        },
        "version" : {
          "name" : "版本",
          "value" : "国行"
        },
        "memoInside" : {
          "name" : "机身内存",
          "value" : "8G"
        },
        "memoCard" : {
          "name" : "内存卡",
          "value" : "64G"
        }
      },
      "thumb" : "img/4.jpg",
      "pics" : [ ],
      "single_barcode" : "1011001013141",
      "sale_price" : 3600,
      "min_sale_price" : 3100,
      "cost_price" : 2800,
      "count" : 2
    },
    {
      "_id" : "fQEXbKSz7DwwuJWAj",
      "catalog" : "手机",
      "barcode" : "2524123456220",
      "properties" : {
        "brand" : {
          "name" : "品牌",
          "value" : "魅族",
          "logo" : "meizu"
        },
        "model" : {
          "name" : "型号",
          "value" : "魅族 MX5(M1530)"
        },
        "color" : {
          "name" : "颜色",
          "value" : "白色"
        },
        "netType" : {
          "name" : "网络",
          "value" : "移动3G"
        },
        "version" : {
          "name" : "版本",
          "value" : "国行"
        },
        "memoInside" : {
          "name" : "机身内存",
          "value" : "32G"
        }
      },
      "thumb" : "img/2.jpg",
      "pics" : [ ],
      "single_barcode" : "6920002010452",
      "sale_price" : 2700,
      "min_sale_price" : 2100,
      "cost_price" : 1500,
      "count" : 2
    }],
    'state':2,
    'state_history':[
        {'date':moment([2016,2,8]).toDate(),'operation':{'state':2,'note':'已下单'},'operator':{'_id':'1','name':'李健'}}
    ],
    'company_id':'1'
  });

  Purchase.insert({
    'order_no':'JH2562352525',
    'date':moment([2015,11,15]).toDate(),
    'total_cost':65262,
    'paid':0,
    'warehouse':{'_id':'11','name':'柜组1'},
    'supplier':{'_id':'1','company':'上海生一科技有限公司'},
    'list':[],
    'state':4,
    'state_history':[
        {'date':moment([2015,12,15]).toDate(),'operation':{'state':2,'note':'已下单'},'operator':{'_id':'1','name':'李健'}},
        {'date':moment([2015,12,16]).toDate(),'operation':{'state':4,'note':'已到货'},'operator':{'_id':'2','name':'刘萌'}}
    ],
    'company_id':'1'
  });

  Purchase.insert({
    'order_no':'JH2562355998',
    'date':moment([2015,12,10]).toDate(),
    'total_cost':118662,
    'paid':18662,
    'warehouse':{'_id':'6','name':'总仓'},
    'supplier':{'_id':'2','company':'深圳市瀚鼎诚科技有限公司'},
    'list':[],
    'state':4,
    'state_history':[
        {'date':moment([2015,12,10]).toDate(),'operation':{'state':2,'note':'已下单'},'operator':{'_id':'1','name':'李健'}},
        {'date':moment([2015,12,11]).toDate(),'operation':{'state':4,'note':'已到货'},'operator':{'_id':'2','name':'刘萌'}}
    ],
    'company_id':'1'
  });

  Purchase.insert({
    'order_no':'JH2562352512',
    'date':moment([2015,11,7]).toDate(),
    'total_cost':211662,
    'paid':211662,
    'warehouse':{'_id':'11','name':'柜组1'},
    'supplier':{'_id':'1','company':'上海生一科技有限公司'},
    'list':[],
    'state':5,
    'state_history':[
        {'date':moment([2015,11,17]).toDate(),'operation':{'state':2,'note':'已下单'},'operator':{'_id':'1','name':'李健'}},
        {'date':moment([2015,11,17]).toDate(),'operation':{'state':4,'note':'已到货'},'operator':{'_id':'2','name':'刘萌'}},
        {'date':moment([2015,11,17]).toDate(),'operation':{'state':5,'note':'已入库'},'operator':{'_id':'','name':''}}
    ],
    'company_id':'1'
  });


  Purchase.insert({
    'order_no':'JH2561342522',
    'date':moment([2015,11,9]).toDate(),
    'total_cost':50632,
    'paid':50632,
    'supplier':{'_id':'3','company':'广州乘风网络技术有限公司'},
    'list':[{
      "_id" : "6M7biTevps2gaz2Ga",
      "catalog" : "手机",
      "barcode" : "9652524125412",
      "properties" : {
        "brand" : {
          "name" : "品牌",
          "value" : "苹果",
          "logo" : "apple"
        },
        "model" : {
          "name" : "型号",
          "value" : "iphone 6 plus(A1530)"
        },
        "color" : {
          "name" : "颜色",
          "value" : "白色"
        },
        "netType" : {
          "name" : "网络",
          "value" : "移动4G"
        },
        "version" : {
          "name" : "版本",
          "value" : "国行"
        },
        "memoInside" : {
          "name" : "机身内存",
          "value" : "64G"
        }
      },
      "thumb" : "img/1.jpg",
      "pics" : [ ],
      "single_barcode" : "6920002010232",
      "sale_price" : 6200,
      "min_sale_price" : 5500,
      "cost_price" : 5188,
      "count" : 4
    }],
    'state':4,
    'warehouse':{'_id':'11','name':'柜组1'},
    'state_history':[
        {'date':moment([2015,11,9]).toDate(),'operation':{'state':2,'note':'已下单'},'operator':{'_id':'1','name':'李健'}},
        {'date':moment([2015,11,13]).toDate(),'operation':{'state':4,'note':'已到货'},'operator':{'_id':'2','name':'刘萌'}}
    ],
    'return':{'state':'部分退','return_order':''},
    'company_id':'1'
  });



  Purchase.insert({
    'order_no':'JH2559342515',
    'date':moment([2015,11,2]).toDate(),
    'total_cost':44635,
    'paid':44635,
    'warehouse':{'_id':'6','name':'总仓'},
    'supplier':{'_id':'3','company':'郑州中尚票务服务有限公司'},
    'list':[],
    'state':5,
    'state_history':[
        {'date':moment([2015,11,2]).toDate(),'operation':{'state':2,'note':'已下单'},'operator':{'_id':'1','name':'李健'}},
        {'date':moment([2015,11,2]).toDate(),'operation':{'state':3,'note':'配送中'},'operator':{'_id':'','name':''}},
        {'date':moment([2015,11,3]).toDate(),'operation':{'state':4,'note':'已到货'},'operator':{'_id':'','name':''}},
        {'date':moment([2015,11,4]).toDate(),'operation':{'state':5,'note':'已入库'},'operator':{'_id':'','name':''}}
    ],
    'return':{'state':'全退','return_order':''},
    'company_id':'1'
  });
}
